home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0089_Pascal Reals to Float.pas < prev    next >
Pascal/Delphi Source File  |  1995-02-28  |  3KB  |  71 lines

  1. Program DoubConv;
  2. {$N+}
  3.  
  4. { This program demonstrates the conversion of a 8 byte Turbo Pascal  }
  5. { real variable type by re-creating the exponent and Mantissa.       }
  6. { The temporary variable Mantissa accumulates the value stored in    }
  7. { bytes 2 through 8.                                                 }
  8.  
  9.  
  10. type EightByteArray = array[1..8] of byte;
  11.      UserReal = Extended; { Insert appropriate type to convert 8 byte
  12.                             real type to.                            }
  13.  
  14. var r : double;
  15.     s : EightByteArray absolute r;
  16.                         { Allow access to individual real type bytes }
  17.     i,j : byte;
  18.     PosFlag : boolean;
  19.     Mantissa : UserReal;
  20.     Exponent : integer;
  21.     Number : UserReal;
  22.  
  23.     { the mantissa and number can be typed as to the number desired  }
  24.     { i.e. Real, Single, Extended, BCD                               }
  25.  
  26. function power (x,y : integer) : UserReal;
  27. begin
  28.   power := exp(y * ln(x));
  29. end;
  30.  
  31. begin
  32.   write('Enter floating point Number ');
  33.   readln(r);
  34.   PosFlag := ($80 and s[8]) = 0;
  35.                    { Check if entry is positive from bit 7 of byte 8 }
  36.   s[8] := s[8] and $7F;                   { Force bit 7 of byte 8 off }
  37.   Mantissa := 0.0;                         { Initialize the Mantissa }
  38.   for i := 1 to 6 do                   { Check each byte of mantissa }
  39.     for j := 0 to 7 do                              { Check each bit }
  40.       if ((s[i] shr j) and 1 ) = 1 then
  41.         Mantissa := Mantissa + power(2, (j + (i-1) * 8));
  42.                                   { Increment mantissa appropriately }
  43.   for j := 0 to 3 do              { get mantissa info from byte 7    }
  44.     if ((s[7] shr j) and 1) = 1 then
  45.       Mantissa := Mantissa + power(2, (j + 48));
  46.                                   { Increment mantissa appropriately }
  47.   { add the assumed mantissa value at bit 52                         }
  48.   Mantissa := (Mantissa + power(2,52)) / power(2,52);
  49.                           { Normalize the number by dividing by 2^52 }
  50.  
  51.   Exponent := s[7] shr 4;
  52.   Exponent := Exponent + s[8] * 16;
  53.  
  54.   if (Exponent > 0) and (Exponent < 2047) then begin
  55.     Exponent := Exponent - 1023;
  56.     Number := Mantissa * power(2, exponent);
  57.                    { Get number by multiply Mantissa by the exponent }
  58.   end
  59.   else
  60.     if (Exponent = 0) then begin
  61.       if mantissa <> 0 then
  62.         Number := Mantissa * power(2, -1022);
  63.     end
  64.     else number := 0;
  65.   if not PosFlag then Number := Number * -1;
  66.   writeln(Number);
  67.   readln;
  68. end.
  69.  
  70.  
  71.